package jjn.carl.backtrack;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Jjn
 * @since 2023/7/29 10:24
 */
public class LeetCode131_2 {
    public List<List<String>> partition(String s) {
        List<List<String>> result = new ArrayList<>();
        backtrack(s, 0, result, new ArrayList<>());
        return result;
    }
    
    private void backtrack(String s, int startIndex, List<List<String>> result, List<String> path) {
        if (startIndex >= s.length()) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            if (isTarget(s, startIndex, i)) {
                path.add(s.substring(startIndex, i - startIndex + 1));
                backtrack(s, i + 1, result, path);
                path.remove(path.size() - 1);
            }
        }
    }
    
    private boolean isTarget(String s, int start, int end) {
        if (s.length() <= 1) {
            return true;
        }
        while (start < end) {
            if (s.charAt(start) != s.charAt(end)) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();
        List<List<String>> list = new LeetCode131_2().partition(nextLine);
        System.out.println(String.join(",", list.toString()));
    }
}
